﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;

namespace Data_Access_Tier
{
    public class DIC_ALLOWANCEData : DatabaseObjectBase
    {
        public DIC_ALLOWANCEData()
        {
            Initialize(true);
        }
        public DIC_ALLOWANCEData(Boolean newConnection)
        {
            Initialize(newConnection);
        }

        public Int32 DIC_ALLOWANCE_Insert(String AllowanceCode, String AllowanceName, Decimal MaximumMoney, String Description)
        {
            try
            {
                if (_dbconn.State != ConnectionState.Open)
                    _dbconn.Open();
                _dbadapter.InsertCommand = new SqlCommand("DIC_ALLOWANCE_Insert");
                _dbadapter.InsertCommand.CommandType = CommandType.StoredProcedure;
                _dbadapter.InsertCommand.Connection = _dbconn;
                _trans = _dbconn.BeginTransaction();
                _dbadapter.InsertCommand.Transaction = _trans;
                _dbadapter.InsertCommand.Parameters.Clear();
                _dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@AllowanceCode", SqlDbType.VarChar, 20));
                _dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@AllowanceName", SqlDbType.NVarChar, 100));
                _dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@MaximumMoney", SqlDbType.Money));
                _dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Description", SqlDbType.NVarChar, 255));
                _dbadapter.InsertCommand.Prepare();
                SqlString AllowanceCodeSQL = AllowanceCode;
                _dbadapter.InsertCommand.Parameters["@AllowanceCode"].Value = AllowanceCodeSQL;
                SqlString AllowanceNameSQL = AllowanceName;
                _dbadapter.InsertCommand.Parameters["@AllowanceName"].Value = AllowanceNameSQL;
                SqlMoney MaximumMoneySQL = MaximumMoney;
                _dbadapter.InsertCommand.Parameters["@MaximumMoney"].Value = MaximumMoneySQL;
                SqlString DescriptionSQL = Description;
                _dbadapter.InsertCommand.Parameters["@Description"].Value = DescriptionSQL;
                Int32 returnvalue = Convert.ToInt32(_dbadapter.InsertCommand.ExecuteScalar());

                _trans.Commit();
                return returnvalue;
            }
            catch (SqlException e)
            {
                try
                {
                    _trans.Rollback();
                }
                catch (SqlException ex)
                {
                    throw new Exception("SQL Rollback Error: DIC_ALLOWANCE_Insert: " + ex.Message, ex);
                }
                throw new Exception("SQL Error: DIC_ALLOWANCE_Insert: " + e.Message, e);
            }
            finally
            {
                if (_dbadapter.InsertCommand != null)
                    _dbadapter.InsertCommand.Dispose();
                CloseConnection();
            }
        }
        public Int32 DIC_ALLOWANCE_Update(String AllowanceCode, String AllowanceName, Decimal MaximumMoney, String Description)
        {
            try
            {
                if (_dbconn.State != ConnectionState.Open)
                    _dbconn.Open();
                _dbadapter.UpdateCommand = new SqlCommand("DIC_ALLOWANCE_Update");
                _dbadapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
                _dbadapter.UpdateCommand.Connection = _dbconn;

                _trans = _dbconn.BeginTransaction();
                _dbadapter.UpdateCommand.Transaction = _trans;

                _dbadapter.UpdateCommand.Parameters.Clear();
                _dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@AllowanceCode", SqlDbType.VarChar, 20));
                _dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@AllowanceName", SqlDbType.NVarChar, 100));
                _dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@MaximumMoney", SqlDbType.Money));
                _dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Description", SqlDbType.NVarChar, 255));
                _dbadapter.UpdateCommand.Prepare();
                SqlString AllowanceCodeSQL = AllowanceCode;
                _dbadapter.UpdateCommand.Parameters["@AllowanceCode"].Value = AllowanceCodeSQL;
                SqlString AllowanceNameSQL = AllowanceName;
                _dbadapter.UpdateCommand.Parameters["@AllowanceName"].Value = AllowanceNameSQL;
                SqlMoney MaximumMoneySQL = MaximumMoney;
                _dbadapter.UpdateCommand.Parameters["@MaximumMoney"].Value = MaximumMoneySQL;
                SqlString DescriptionSQL = Description;
                _dbadapter.UpdateCommand.Parameters["@Description"].Value = DescriptionSQL;
                Int32 returnvalue = Convert.ToInt32(_dbadapter.UpdateCommand.ExecuteScalar());

                _trans.Commit();
                return returnvalue;
            }
            catch (SqlException e)
            {
                try
                {
                    _trans.Rollback();
                }
                catch (SqlException ex)
                {
                    throw new Exception("SQL Rollback Error: DIC_ALLOWANCE_Update: " + ex.Message, ex);
                }
                throw new Exception("SQL Error: DIC_ALLOWANCE_Update: " + e.Message, e);
            }
            finally
            {
                if (_dbadapter.UpdateCommand != null)
                    _dbadapter.UpdateCommand.Dispose();
                CloseConnection();
            }
        }
        public Int32 DIC_ALLOWANCE_Disable(String AllowanceCode, ref Boolean Enabled)
        {
            try
            {
                if (_dbconn.State != ConnectionState.Open)
                    _dbconn.Open();
                _dbadapter.UpdateCommand = new SqlCommand("DIC_ALLOWANCE_Disable");
                _dbadapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
                _dbadapter.UpdateCommand.Connection = _dbconn;

                _trans = _dbconn.BeginTransaction();
                _dbadapter.UpdateCommand.Transaction = _trans;

                _dbadapter.UpdateCommand.Parameters.Clear();
                _dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@AllowanceCode", SqlDbType.VarChar, 20));
                _dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@enabled", SqlDbType.Bit));
                _dbadapter.UpdateCommand.Prepare();
                SqlString AllowanceCodeSQL = AllowanceCode;
                _dbadapter.UpdateCommand.Parameters["@AllowanceCode"].Value = AllowanceCodeSQL;
                _dbadapter.UpdateCommand.Parameters["@enabled"].Direction = ParameterDirection.Output;
                Int32 returnvalue = Convert.ToInt32(_dbadapter.UpdateCommand.ExecuteScalar());

                _trans.Commit();
                return returnvalue;
            }
            catch (SqlException e)
            {
                try
                {
                    _trans.Rollback();
                }
                catch (SqlException ex)
                {
                    throw new Exception("SQL Rollback Error: DIC_ALLOWANCE_Disable: " + ex.Message, ex);
                }
                throw new Exception("SQL Error: DIC_ALLOWANCE_Disable: " + e.Message, e);
            }
            finally
            {
                if (_dbadapter.UpdateCommand != null)
                    _dbadapter.UpdateCommand.Dispose();
                CloseConnection();
            }
        }
        public Int32 DIC_ALLOWANCE_Delete(String AllowanceCode)
        {
            try
            {
                if (_dbconn.State != ConnectionState.Open)
                    _dbconn.Open();
                _dbadapter.DeleteCommand = new SqlCommand("DIC_ALLOWANCE_Delete");
                _dbadapter.DeleteCommand.CommandType = CommandType.StoredProcedure;
                _dbadapter.DeleteCommand.Connection = _dbconn;

                _trans = _dbconn.BeginTransaction();
                _dbadapter.DeleteCommand.Transaction = _trans;

                _dbadapter.DeleteCommand.Parameters.Clear();
                _dbadapter.DeleteCommand.Parameters.Add(new SqlParameter("@AllowanceCode", SqlDbType.VarChar, 20));
                _dbadapter.DeleteCommand.Prepare();
                SqlString AllowanceCodeSQL = AllowanceCode;
                _dbadapter.DeleteCommand.Parameters["@AllowanceCode"].Value = AllowanceCodeSQL;
                Int32 returnvalue = Convert.ToInt32(_dbadapter.DeleteCommand.ExecuteScalar());

                _trans.Commit();
                return returnvalue;
            }
            catch (SqlException e)
            {
                try
                {
                    _trans.Rollback();
                }
                catch (SqlException ex)
                {
                    throw new Exception("SQL Rollback Error: DIC_ALLOWANCE_Delete: " + ex.Message, ex);
                }
                throw new Exception("SQL Error: DIC_ALLOWANCE_Delete: " + e.Message, e);
            }
            finally
            {
                if (_dbadapter.DeleteCommand != null)
                    _dbadapter.DeleteCommand.Dispose();
                CloseConnection();
            }
        }
        public Int32 DIC_ALLOWANCE_Save(String AllowanceCode, String AllowanceName, Decimal MaximumMoney, String Description)
        {
            try
            {
                if (_dbconn.State != ConnectionState.Open)
                    _dbconn.Open();
                _dbadapter.UpdateCommand = new SqlCommand("DIC_ALLOWANCE_Save");
                _dbadapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
                _dbadapter.UpdateCommand.Connection = _dbconn;

                _trans = _dbconn.BeginTransaction();
                _dbadapter.UpdateCommand.Transaction = _trans;

                _dbadapter.UpdateCommand.Parameters.Clear();
                _dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@AllowanceCode", SqlDbType.VarChar, 20));
                _dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@AllowanceName", SqlDbType.NVarChar, 100));
                _dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@MaximumMoney", SqlDbType.Money));
                _dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Description", SqlDbType.NVarChar, 255));
                _dbadapter.UpdateCommand.Prepare();
                SqlString AllowanceCodeSQL = AllowanceCode;
                _dbadapter.UpdateCommand.Parameters["@AllowanceCode"].Value = AllowanceCodeSQL;
                SqlString AllowanceNameSQL = AllowanceName;
                _dbadapter.UpdateCommand.Parameters["@AllowanceName"].Value = AllowanceNameSQL;
                SqlMoney MaximumMoneySQL = MaximumMoney;
                _dbadapter.UpdateCommand.Parameters["@MaximumMoney"].Value = MaximumMoneySQL;
                SqlString DescriptionSQL = Description;
                _dbadapter.UpdateCommand.Parameters["@Description"].Value = DescriptionSQL;
                Int32 returnvalue = Convert.ToInt32(_dbadapter.UpdateCommand.ExecuteScalar());

                _trans.Commit();
                return returnvalue;
            }
            catch (SqlException e)
            {
                try
                {
                    _trans.Rollback();
                }
                catch (SqlException ex)
                {
                    throw new Exception("SQL Rollback Error: DIC_ALLOWANCE_Save: " + ex.Message, ex);
                }
                throw new Exception("SQL Error: DIC_ALLOWANCE_Save: " + e.Message, e);
            }
            finally
            {
                if (_dbadapter.UpdateCommand != null)
                    _dbadapter.UpdateCommand.Dispose();
                CloseConnection();
            }
        }
        public DataSet DIC_ALLOWANCE_GetList()
        {
            try
            {
                if (_dbconn.State != ConnectionState.Open)
                    _dbconn.Open();
                _dbadapter.SelectCommand = new SqlCommand("DIC_ALLOWANCE_GetList");
                _dbadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                _dbadapter.SelectCommand.Connection = _dbconn;
                _dbadapter.SelectCommand.Parameters.Clear();
                _dbadapter.SelectCommand.Prepare();
                _dbadapter.Fill(_data);

                return _data;
            }
            catch (SqlException e)
            {
                throw new Exception("SQL Error: DIC_ALLOWANCE_GetList: " + e.Message, e);
            }
            finally
            {
                if (_dbadapter.SelectCommand != null)
                    _dbadapter.SelectCommand.Dispose();
                CloseConnection();
            }
        }
        public DataSet DIC_ALLOWANCE_Get(String AllowanceCode)
        {
            try
            {
                if (_dbconn.State != ConnectionState.Open)
                    _dbconn.Open();
                _dbadapter.SelectCommand = new SqlCommand("DIC_ALLOWANCE_Get");
                _dbadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                _dbadapter.SelectCommand.Connection = _dbconn;
                _dbadapter.SelectCommand.Parameters.Clear();
                _dbadapter.SelectCommand.Parameters.Add(new SqlParameter("@AllowanceCode", SqlDbType.VarChar, 20));
                _dbadapter.SelectCommand.Prepare();
                SqlString AllowanceCodeSQL = AllowanceCode;
                _dbadapter.SelectCommand.Parameters["@AllowanceCode"].Value = AllowanceCodeSQL;
                _dbadapter.Fill(_data);

                return _data;
            }
            catch (SqlException e)
            {
                throw new Exception("SQL Error: DIC_ALLOWANCE_Get: " + e.Message, e);
            }
            finally
            {
                if (_dbadapter.SelectCommand != null)
                    _dbadapter.SelectCommand.Dispose();
                CloseConnection();
            }
        }

        public DataSet DIC_ALLOWANCE_Search(String AllowanceCode, String AllowanceName, Decimal MaximumMoney, String Description)
        {
            try
            {
                if (_dbconn.State != ConnectionState.Open)
                    _dbconn.Open();
                _dbadapter.SelectCommand = new SqlCommand("DIC_ALLOWANCE_Search");
                _dbadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                _dbadapter.SelectCommand.Connection = _dbconn;
                _dbadapter.SelectCommand.Parameters.Clear();
                _dbadapter.SelectCommand.Parameters.Add(new SqlParameter("@AllowanceCode", SqlDbType.VarChar, 20));
                _dbadapter.SelectCommand.Parameters.Add(new SqlParameter("@AllowanceName", SqlDbType.NVarChar, 100));
                _dbadapter.SelectCommand.Parameters.Add(new SqlParameter("@MaximumMoney", SqlDbType.Money));
                _dbadapter.SelectCommand.Parameters.Add(new SqlParameter("@Description", SqlDbType.NVarChar, 255));
                _dbadapter.SelectCommand.Prepare();
                SqlString AllowanceCodeSQL = AllowanceCode;
                SqlString AllowanceNameSQL = AllowanceName;
                SqlMoney MaximumMoneySQL = MaximumMoney;
                SqlString DescriptionSQL = Description;
                _dbadapter.SelectCommand.Parameters["@AllowanceCode"].Value = AllowanceCodeSQL;
                _dbadapter.SelectCommand.Parameters["@AllowanceName"].Value = AllowanceNameSQL;
                _dbadapter.SelectCommand.Parameters["@MaximumMoney"].Value = MaximumMoneySQL;
                _dbadapter.SelectCommand.Parameters["@Description"].Value = DescriptionSQL;
                _dbadapter.Fill(_data);

                return _data;
            }
            catch (SqlException e)
            {
                throw new Exception("SQL Error: DIC_ALLOWANCE_Search: " + e.Message, e);
            }
            finally
            {
                if (_dbadapter.SelectCommand != null)
                    _dbadapter.SelectCommand.Dispose();
                CloseConnection();
            }
        }
    }
}
